package com.ys.controller.web.storage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.storage.TccAllocateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.TccAllocate;
import com.ys.entity.storage.TccAllocateDetail;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.enume.storage.TccAllocateEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/storage/tccAllocate")
public class WebTccAllocateController {
	
	@Autowired
	private CommonApiService<TccAllocate> commonApiService;


	@Autowired
	private TccAllocateService tccAllocateService;
	
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TCC_Allocate);
			
			TccAllocate entity = new TccAllocate()
					.setBid(bid)
					.setType(TccAllocateEnum.Type.PU_PIN.value)
					.setCreateTime(new Date());
			
			List<Map<String, Object>> sonlist = new ArrayList<>();
			
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCC_Allocate, sonlist);
			
			return commonApiService.toAdd(model, entity, MenuId.TCC_Allocate, null, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 审核
	 */
	@GetMapping("/toFlow")
	public String toFlow(Model model, HttpServletRequest request, HttpServletResponse response,
			String bid){
		try {
			if (bid == null || bid.length() == 0) {
				return W.error(model, "单号不能为空");
			}

			TccAllocate entity = new TccAllocate().setBid(bid).queryFirst();

			List<Map<String, Object>> sonlist = tccAllocateService.getItemList(bid);
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCC_Allocate, sonlist);

			YsProcInst yspProcInst = new YsProcInst().setBid(bid).queryFirst();
			if(yspProcInst.getStateFlow() != YsProcInstEnum.StateFlow.INIT.value) {
				subGridConfig.setButtonArr(new ArrayList<SubGridConfigDto.ButtonDto>());
			}


			return commonApiService.toEdit(model, entity.getId(), new TccAllocate(), MenuId.TCC_Allocate, null, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
}